package ThreadDemo;

public class synchronizedDemo2 {
    public static void main(String[] args) {
        Shop s = new Shop();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                s.buy(); //调用shop中的by方法进行测试
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                s.buy();
            }
        };

        t1.start();
        t2.start();
    }
}

class Shop {
    public void buy() {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + ":在购物......");
            Thread.sleep(2000);

            synchronized (this) {
                System.out.println(t.getName() + ":在试衣服！！！！");
                Thread.sleep(2000);
            }

            System.out.println(t.getName() + "结账离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
